package com.qcby.demo;

import java.lang.reflect.Proxy;

public class TransactionDemo {
    public static void main(String[] args) {
        TransferService transferService = new TransferServiceImpl();

        // 创建动态代理
        TransferService proxy = (TransferService) Proxy.newProxyInstance(
                transferService.getClass().getClassLoader(),
                transferService.getClass().getInterfaces(),
                new TransactionProxy(transferService)
        );

        // 执行转账操作
        try {
            proxy.transfer("张三", "李四", 500); // 正常转账
            proxy.transfer("张三", "李四", 1500); // 将引发异常，触发回滚
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }
}
